/**
 * Player instance
 */
app._player = null;

/**
 * Units module
 */
app.units = {
	init : function() {
		app._player = app.UnitsMgr.createPlayer();
	}
};

/**
 * Units factory class
 */
app.UnitsMgr = {
	// Unit types cache
	_types : {},
	
	/**
	 * Create unit of given type
	 *
	 * @param type Unit type; if a string given then get the actual type object from cache
	 * @return Unit
	 */
	createUnit : function(type) {
		if (typeof type != 'object') {
			type = this.getType(type);
		}
		
		return $.extend(true, {}, app.base.unit, type);
	},
	
	createPlayer : function() {
		return this.createUnit('player');
	},
	
	/**
	 * Get unit type object from cache
	 *
	 * @param type Unit type
	 * @return Unit type object
	 */
	getType : function(type) {
		return this._types[type];
	}
};

/**
 * Basic unit
 */
app.base.unit = {
	name : '',
	damage : 0,
	health : 0,
	
	/**
	 * Get unit damage
	 *
	 * @return Damage value
	 */
	getDamage : function() {
		return this.damage;
	},
	
	/**
	 * Check if unit is dead
	 *
	 * @return 'true' if unit is dead
	 */
	isDead : function() {
		return this.health <= 0;
	}
};

/**
 * Data initialization
 */
(function() {
	var types = {
		'base' : {
			damage : 1,
			accuracy : .5,
			health : 10
		},
		'player' : {
			base : 'base',
			health : 50,
			name : 'Player'
		},
		
		'mob' : {
			base : 'base'
		},
		'rat' : {
			base : 'mob',
			name : 'Rat'
		}
	};
	
	// Loop though unit types and add them to unit types cache
	for (var id in types) {
		var type, baseType;
		
		type = types[id];
		
		// If unit type has a base type then get it and delete property from new type
		if (type.base) {
			baseType = app.UnitsMgr.getType(type.base);
			
			delete type.base;
		}
		
		baseType = baseType || app.base.unit;
		
		app.UnitsMgr._types[id] = $.extend(true, {}, baseType, type);
	}
})();